package yzserver.sx4a.role;

import com.sitech.portal.system.dao.ISysRoleDao;
import com.sitech.portal.system.dao.ISysRoleModuleDao;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.portal.system.domain.TblSYS_ROLE_MODULE;
import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IRoleDao;
import yzserver.sx4a.domain.RoleDomain;
import yzserver.sx4a.form.Role;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aRoleImpl implements Sx4aRole {

    private IRoleDao roleDao;
    private ISysRoleDao sysRoleDao;
    private ISysRoleModuleDao sysRoleModuleDao;

    public void setSysRoleDao(ISysRoleDao sysRoleDao) {
        this.sysRoleDao = sysRoleDao;
    }

    public void setSysRoleModuleDao(ISysRoleModuleDao sysRoleModuleDao) {
        this.sysRoleModuleDao = sysRoleModuleDao;
    }

    public void setRoleDao(IRoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Override
    public Role selRole(String roleid) {
        RoleDomain domain = roleDao.selRole(roleid);
        List<String> modules = sysRoleModuleDao.findModuleIdByRoleId(roleid);
        if(domain!=null){
            if(modules!=null&&modules.size()>0){
                StringBuffer sb = new StringBuffer("");
                for(String m :modules){
                    sb.append(m+"@");
                }
                sb.delete(sb.length() - 1, sb.length());
                domain.setPERMISSIONID(sb.toString());
            }
            Role role = new Role(domain);
            return role;
        }else{
            return null;
        }
    }

    @Override
    public Ret addRole(Role role) {
        try {
            roleDao.addRole(new RoleDomain(role));
            sysRoleModuleDao.deleteByRoleId(role.getRoleid());
            String pm = role.getPermissionid();
            String pmArray[] = null;
            if(pm!=null){
                pmArray = pm.split("@");
            }
            if(pmArray!=null&&pmArray.length>0){
                for (int i = 0; i <pmArray.length; i++) {
                    TblSYS_ROLE_MODULE obj = new TblSYS_ROLE_MODULE();
                    obj.setROLE_ID(role.getRoleid());
                    obj.setMODULE_ID(pmArray[i]);
                    sysRoleModuleDao.insert(obj);
                }
            }
        } catch (Exception e) {
            return RetTools.getRet("创建角色失败", e);
        }

        //portal
        TblSYS_ROLE obj = new TblSYS_ROLE();
        obj.setROLE_ID(role.getRoleid());
        obj.setROLE_NAME(role.getRolename());
        obj.setROLE_DESC(role.getRoledes());
        obj.setPARENT_ID(role.getRoleparent());
        try {
            sysRoleDao.add(obj);
        } catch (Exception e) {
            return RetTools.getRet("创建角色失败", e);
        }

        return RetTools.getRet("创建角色成功", null);
    }

    @Override
    public Ret editRole(Role role) {
        try {
            roleDao.editRole(new RoleDomain(role));
            sysRoleModuleDao.deleteByRoleId(role.getRoleid());
            String pm = role.getPermissionid();
            String pmArray[] = null;
            if(pm!=null){
                pmArray = pm.split("@");
            }
            if(pmArray!=null&&pmArray.length>0){
                for (int i = 0; i <pmArray.length; i++) {
                    TblSYS_ROLE_MODULE obj = new TblSYS_ROLE_MODULE();
                    obj.setROLE_ID(role.getRoleid());
                    obj.setMODULE_ID(pmArray[i]);
                    sysRoleModuleDao.insert(obj);
                }
            }
        } catch (Exception e) {
            return RetTools.getRet("修改角色失败", e);
        }
        //portal
        TblSYS_ROLE obj = new TblSYS_ROLE();
        obj.setROLE_ID(role.getRoleid());
        obj.setROLE_NAME(role.getRolename());
        obj.setROLE_DESC(role.getRoledes());
        obj.setPARENT_ID(role.getRoleparent());
        try {
            sysRoleDao.update(obj);
        } catch (Exception e) {
            return RetTools.getRet("修改角色失败", e);
        }
        return RetTools.getRet("修改角色成功", null);
    }

    @Override
    public Ret delRole(String roleid) {
        try {
            roleDao.delRole(roleid);
        } catch (Exception e) {
            return RetTools.getRet("删除角色失败", e);
        }
        //portal
        try {
            sysRoleDao.deleteRole(roleid);
            sysRoleModuleDao.deleteByRoleId(roleid);
        } catch (Exception e) {
            return RetTools.getRet("删除角色失败", e);
        }
        return RetTools.getRet("删除角色成功", null);

    }

    @Override
    public List selAllRole(String roleid) {
        List<RoleDomain> roleDomains = new ArrayList<RoleDomain>();
        List  roleList = new ArrayList();
        if("all".equalsIgnoreCase(roleid)){ // all为接口约定的固定值
            roleDomains = roleDao.selAllRole(roleid);
            if(roleDomains!=null){
                for (int i = 0; i <roleDomains.size(); i++) {
                    Role role = new Role(roleDomains.get(i));
                    roleList.add(role);
                }
            }
        }
        return roleList;
    }
}
